package com.zaimokuza.cn.controller;

import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.Blog;
import com.zaimokuza.cn.entity.response.ResponseFormat;
import com.zaimokuza.cn.service.BlogService;
import com.zaimokuza.cn.util.RandomUtils;
import com.zaimokuza.cn.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Api(tags = "博客相关接口")
@ResponseFormat
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Resource
    private BlogService blogService;

    @ApiOperation(value = "添加博客", notes = "添加博客，对属性会进行校验")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public String addBlog(@Validated @RequestBody Blog blog) {
        // 博客ID
        String uid = RandomUtils.get32Uid();
        blog.setId(uid);
        // 获取当前用户ID作为作者ID
        blog.setAuthorId(UserUtils.getUserId());
        // 添加博客
        blogService.addBlog(blog);
        // 返回博客ID
        return uid;
    }

    @ApiOperation(value = "更新博客", notes = "更新博客，对属性会进行校验")
    @PutMapping("/update")
    @Transactional(rollbackFor = Exception.class)
    public void updateBlog(@Validated @RequestBody Blog blog) {
        blogService.updateBlog(blog, UserUtils.getUserId());
    }

    @ApiOperation(value = "增加博客阅读量", notes = "增加博客阅读量")
    @PutMapping("/view/{blogId}")
    public void viewBlog(@PathVariable @ApiParam("博客ID") String blogId) {
        blogService.increaseBlogPageViews(blogId);
    }

    @ApiOperation(value = "用户删除博客", notes = "用户根据ID删除博客")
    @DeleteMapping("/delete")
    @Transactional(rollbackFor = Exception.class)
    public void deleteBlogForUser(@RequestParam @ApiParam("博客ID") String id) {
        blogService.deleteBlogForUser(id, UserUtils.getUserId());
    }

    @ApiOperation(value = "管理端删除博客", notes = "管理端根据ID删除博客")
    @DeleteMapping("/manageDelete")
    @Transactional(rollbackFor = Exception.class)
    public void deleteBlogForManage(@RequestParam @ApiParam("博客ID") String id) {
        blogService.deleteBlog(id);
    }

    @ApiOperation(value = "查询分页的博客列表", notes = "根据作者域名分页查询分页的博客列表")
    @GetMapping("/page")
    public PageResult<Blog> getBlogPage(@RequestParam(required = false) @ApiParam("域名") String authorDomain,
                                        @RequestParam(required = false) @ApiParam("分类") String classificationId,
                                        @RequestParam(required = false) @ApiParam("搜索内容") String search,
                                        @RequestParam(defaultValue = "1") @ApiParam("当前页") Integer page,
                                        @RequestParam(defaultValue = "10") @ApiParam("每页条数") Integer size) {
        // 整理条件
        Blog param = new Blog();
        param.setAuthorDomain(authorDomain);
        param.setClassificationId(classificationId);
        param.setTitle(search);
        param.setSummary(search);
        // 查询
        return blogService.getBlogPage(param, page, size);
    }

    @ApiOperation(value = "根据博客ID查询博客详情", notes = "根据博客ID查询博客详情")
    @GetMapping("/detail")
    public Blog getBlogDetail(@RequestParam @ApiParam("博客ID") String id) {
        return blogService.getBlogDetail(id, UserUtils.getUser() == null ? null : UserUtils.getUserId());
    }

    @ApiOperation(value = "根据博客ID查询博客详情", notes = "根据博客ID查询博客详情")
    @GetMapping("/updateDetail")
    public Blog getBlogDetailForUpdate(@RequestParam @ApiParam("博客ID") String id) {
        return blogService.getBlogDetailForUpdate(id, UserUtils.getUserId());
    }

    @ApiOperation(value = "查询推荐博客", notes = "查询推荐博客")
    @GetMapping("/recommend")
    public ArrayList<Blog> getRecommendBlog(@RequestParam(required = false) @ApiParam("博客发布时间") String time) {
        return blogService.getRecommendBlogList(time);
    }

    @ApiOperation(value = "搜索博客", notes = "搜索博客")
    @GetMapping("/search")
    public ArrayList<Blog> getSearchBlog(@RequestParam @ApiParam("搜索内容") String str,
                                         @RequestParam(required = false) @ApiParam("博客发布时间") String time) {
        return blogService.getSearchBlogList(str, time);
    }

}
